package com.ldatum.crm.serviceImpl;

import java.util.List;

import com.ldatum.crm.dao.TEmployeeDao;
import com.ldatum.crm.daoImpl.TEmployeeDaoImpl;
import com.ldatum.crm.domain.TEmployee;
import com.ldatum.crm.service.TEmployeeService;
import com.ldatum.crm.util.MD5;

public class TEmployeeServiceImpl implements TEmployeeService {

	TEmployeeDao employeeDao = new TEmployeeDaoImpl();
	@Override
	public void add(TEmployee TEmployee) throws Exception {
		employeeDao.add(TEmployee);
	}

	@Override
	public void delete(TEmployee TEmployee, int id) throws Exception {
		employeeDao.delete(TEmployee, id);
	}

	@Override
	public void update(TEmployee TEmployee, int id) throws Exception {
		employeeDao.update(TEmployee, id);
	}

	@Override
	public TEmployee findById(Class<TEmployee> clazz,int id) throws Exception {
		return employeeDao.findById(clazz,id);
	}

	@Override
	public TEmployee findByLoginname(Class<TEmployee> clazz, String loginname)
			throws Exception {
		return employeeDao.findByLoginname(clazz,loginname);
	}

	@Override
	public List<TEmployee> list(Class<TEmployee> clazz) throws Exception {
		return employeeDao.list(clazz);
	}

	@Override
	public void save(TEmployee employee) throws Exception {
		employee.setPassword(MD5.md5(employee.getPassword()));
		employeeDao.save(employee);
	}

	@Override
	public void deletes(String ids) throws Exception {
		ids = ids.substring(0, ids.length()-1);
		employeeDao.deletes(ids);
	}

	@Override
	public void resetPasswd(String ids, String npwd) throws Exception {
		ids = ids.substring(0, ids.length()-1);
		employeeDao.resetPasswd(ids,npwd);
	}

	@Override
	public String changepwd(Integer id, String oldpwd, String newpwd,
			String rpnewpwd) throws Exception {
		if(newpwd.equals(rpnewpwd)){
			if(MD5.md5(oldpwd).equals(findById(TEmployee.class,id).getPassword())){
				employeeDao.changepwd(id,newpwd);
				return "修改密码成功";
			}else{
				return "输入的原始密码错误";
			}
		}else{
			return "两次输入的密码不一致";
		}
	}
}
